package com.example.demo.controller;

import com.example.demo.entity.MusicCollection;
import com.example.demo.entity.MusicQuery;
import com.example.demo.entity.NoteCollection;
import com.example.demo.service.MusicCollectionService;

import com.example.demo.vo.ViewMusicCollectionInfo;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("mc")
public class MusicCollectionController {
    @Autowired
    private MusicCollectionService musicCollectionService;



    @GetMapping("/g1")
    public PageInfo<ViewMusicCollectionInfo> getMusicCollectionByUser(
            @RequestParam Integer user_id,
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "size", defaultValue = "8") Integer pageSize,
            @RequestParam(value = "music_name", required = false) String musicName,
            @RequestParam(value = "artist", required = false) String artist,
            @RequestParam(value = "type", required = false) String type) {

        MusicQuery query = new MusicQuery();
        query.setUser_id(user_id);
        query.setMusic_name(musicName);
        query.setArtist(artist);
        query.setType(type);

        return musicCollectionService.findMusicCollectionInfo(query, pageNum, pageSize);
    }

    //根据用户id和笔记id，收藏笔记。
    @PostMapping("/MusicCollectionadd")
    public Map<String, Object> insertMusicCollection(@RequestBody MusicCollection musicCollection) {
        boolean success = musicCollectionService.insertMusicCollection(musicCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);  //响应的success字段
        response.put("message", success ? "收藏成功" : "收藏失败");
        return response;
    }

    // 取消收藏（笔记详情页）
    @DeleteMapping("/MusicCollectiondelete")
    public Map<String, Object> deleteMusicCollection1(@RequestBody MusicCollection musicCollection) {
        boolean success = musicCollectionService.deleteMusicCollection1(musicCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "取消收藏成功" : "取消收藏失败");
        return response;
    }

    //检查是否已经收藏
    @GetMapping("/MusicCollectioncheck")
    public Map<String, Object> checkIfMusicCollection(@RequestParam Integer user_id, @RequestParam Integer music_id) {
        boolean collected = musicCollectionService.checkIfMusicCollection(user_id, music_id);
        Map<String, Object> response = new HashMap<>();
        response.put("collected", collected);
        return response;
        //创建了一个HashMap实例，它实现了Map接口，用于存储键值对。response变量被用作存储HTTP响应数据的容器，
        //其中键是String类型的，而值是Object类型的，这意味着可以在其中存储任何类型的对象。
    }





    // 统计用户收藏的歌曲种类数量
    @GetMapping("/types/count")
    public Map<String, Integer> countMusicTypeByUserId(@RequestParam Integer user_id) {
        return musicCollectionService.countMusicTypeByUserId(user_id);
    }
}
